
package com.jiu.http.fish;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import com.jiu.core.Account;
import com.jiu.util.EnCodeingUtil;
import com.jiu.util.JSONUtils;

public class FishHttp {
	public static final String URL = "http://211.100.28.152/fishingservice/fishingservice.asmx";
	public static final String PutFishingCmd = "http://fishingservice.com/PutFishingCmd";
	public static final String GetQMsg = "http://fishingservice.com/GetQMsg";
	public static final String LogIn = "http://fishingservice.com/LogIn";
	public static final String LogOut = "http://fishingservice.com/LogOut";
	public static final String GetDetailInfo = "http://fishingservice.com/GetDetailInfo";
	public static final String GetMe = "http://fishingservice.com/GetMe";
	public static final String GetFishingBagUsedSlot = "http://fishingservice.com/GetFishingBagUsedSlot";
	public static final String GetFishingBagTable = "http://fishingservice.com/GetFishingBagTable";
	public static final String GetFishingBankUsedSlot = "http://fishingservice.com/GetFishingBankUsedSlot";
	public static final String GetFishingBankTable = "http://fishingservice.com/GetFishingBankTable";
	public static final String GetAuction = "http://fishingservice.com/GetAuction";
	
	/**
	 * content 头
	 * 
	 * @param account
	 * @return
	 */
	private static String getHeadContent(Account account) {
		return "<?xml version=\"1.0\" encoding=\"utf-8\"?><soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Header><AuthHeader xmlns=\"http://fishingservice.com/\">"
				+ "<Username>"
				+ account.getQq()
				+ "</Username><Password>"
				+ account.getPassword()
				+ "</Password><VerifyCode>"
				+ account.getVerifyCode() + "</VerifyCode></AuthHeader></soap:Header><soap:Body>";
	}
	
	public static String GetQMsg(Account account, int id) {
		String content = getHeadContent(account) + "<GetQMsg xmlns=\"http://fishingservice.com/\"><Groupid>" + account.getGroupid() + "</Groupid><startid>"
				+ id + "</startid></GetQMsg></soap:Body></soap:Envelope>";
		return fishingConnection(URL, GetQMsg, content);
	}
	
	public static String PutFishingCmd(Account account, String msg) throws UnsupportedEncodingException {
		Map<String, String> map = new HashMap<String, String>();
		map.put("Font", EnCodeingUtil.URLEncoder(account.getFont()));
		map.put("ForeColor", account.getFishColor());
		map.put("Text", EnCodeingUtil.URLEncoder(msg));
		String content = getHeadContent(account) + "<PutFishingCmd xmlns=\"http://fishingservice.com/\"><Groupid>" + account.getGroupid() + "</Groupid><uin>"
				+ account.getQq() + "</uin>" + "<msg>" + JSONUtils.toJson(map) + "</msg></PutFishingCmd></soap:Body></soap:Envelope>";
		return fishingConnection(URL, PutFishingCmd, content);
	}
	
	public static String LogIn(Account account) {
		String content = getHeadContent(account) + "<LogIn xmlns=\"http://fishingservice.com/\"><uin>" + account.getQq() + "</uin><groupid>"
				+ account.getGroupid() + "</groupid></LogIn></soap:Body></soap:Envelope>";
		return fishingConnection(URL, LogIn, content);
	}
	public static String LogOut(Account account) {
		String content = getHeadContent(account) + "<LogOut xmlns=\"http://fishingservice.com/\"><uin>" + account.getQq() + "</uin><groupid>"
				+ account.getGroupid() + "</groupid></LogOut></soap:Body></soap:Envelope>";
		return fishingConnection(URL, LogOut, content);
	}
	public static String GetDetailInfo(Account account) {
		String content = getHeadContent(account) + "<GetDetailInfo xmlns=\"http://fishingservice.com/\"><uin>" + account.getQq()
				+ "</uin><maxshowskill>1000</maxshowskill></GetDetailInfo></soap:Body></soap:Envelope>";
		return fishingConnection(URL, GetDetailInfo, content);
	}
	
	public static String GetMe(Account account) {
		String content = getHeadContent(account) + "<GetMe xmlns=\"http://fishingservice.com/\"><uin>" + account.getQq()
				+ "</uin></GetMe></soap:Body></soap:Envelope>";
		return fishingConnection(URL, GetMe, content);
	}
	
	public static String GetFishingBagUsedSlot(Account account) {
		String content = getHeadContent(account) + "<GetFishingBagUsedSlot xmlns=\"http://fishingservice.com/\"><uin>" + account.getQq()
				+ "</uin></GetFishingBagUsedSlot></soap:Body></soap:Envelope>";
		return fishingConnection(URL, GetFishingBagUsedSlot, content);
	}
	
	public static String GetFishingBagTable(Account account) {
		String content = getHeadContent(account) + "<GetFishingBagTable xmlns=\"http://fishingservice.com/\"><uin>" + account.getQq()
				+ "</uin></GetFishingBagTable></soap:Body></soap:Envelope>";
		return fishingConnection(URL, GetFishingBagTable, content);
	}
	
	public static String GetFishingBankUsedSlot(Account account) {
		String content = getHeadContent(account) + "<GetFishingBankUsedSlot xmlns=\"http://fishingservice.com/\"><uin>" + account.getQq()
				+ "</uin></GetFishingBankUsedSlot></soap:Body></soap:Envelope>";
		return fishingConnection(URL, GetFishingBankUsedSlot, content);
	}
	
	public static String GetFishingBankTable(Account account) {
		String content = getHeadContent(account) + "<GetFishingBankTable xmlns=\"http://fishingservice.com/\"><uin>" + account.getQq()
				+ "</uin></GetFishingBankTable></soap:Body></soap:Envelope>";
		return fishingConnection(URL, GetFishingBankTable, content);
	}
	
	public static String GetAuction(Account account, String type) {
		String content =  getHeadContent(account) + "<GetAuction xmlns=\"http://fishingservice.com/\"><uin>" + account.getQq() + "</uin><type>" + type
				+ "</type></GetAuction></soap:Body></soap:Envelope>";
		return fishingConnection(URL, GetAuction, content);
	}
	
	
	private static String fishingConnection(String url, String action,String content) {
		String line, str = "";
		HttpURLConnection connection = null;
		try {
			URL postUrl = new URL(url);
			connection = (HttpURLConnection) postUrl.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.addRequestProperty(
					"User-Agent",
					" Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.5477)");
			connection.addRequestProperty("Content-Type",
					" text/xml; charset=utf-8");
			connection.addRequestProperty("Host", " 211.100.28.152");
			connection.addRequestProperty("Expect", "  100-continue");
			connection.addRequestProperty("SOAPAction", " \"" + action + "\"");
			connection.setConnectTimeout(30000);
			connection.setReadTimeout(30000);
			connection.connect();
			DataOutputStream out = new DataOutputStream(
					connection.getOutputStream());
			out.writeBytes(content);
			out.flush();
			out.close(); // flush and close
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					connection.getInputStream(), "utf-8"));
			while ((line = reader.readLine()) != null) {
				str += line;
			}
			reader.close();
			
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			connection.disconnect();			
		}
		return str;
	}
}
